package com.controller;

import java.io.InputStream;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import javax.faces.bean.ManagedBean;
import javax.faces.bean.SessionScoped;
import javax.faces.context.FacesContext;
import javax.servlet.ServletContext;

import net.sf.jxls.exception.ParsePropertyException;

import org.primefaces.event.SelectEvent;
import org.primefaces.model.DefaultStreamedContent;
import org.primefaces.model.DefaultTreeNode;
import org.primefaces.model.StreamedContent;
import org.primefaces.model.TreeNode;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.security.core.userdetails.User;
import org.springframework.stereotype.Component;

import com.dto.Document;
import com.dto.EnumStatus;
import com.entity.IssueEntity;
import com.service.ExcelReportService;
import com.service.JiraRestClientService;
import com.service.JiraService;
import com.service.JiraServiceFromTxt;
import com.service.TicketService;

@Component
@ManagedBean
@SessionScoped
public class TicketController {

	@Autowired
	private ExcelReportService excelReportService;
	@Autowired
	private JiraRestClientService jiraRestClientService;
	@Autowired
	private JiraService jiraService;
	@Autowired
	private JiraServiceFromTxt jiraServiceFromTxt;
	@Autowired
	private TicketService service;
	private List<IssueEntity> ticket = null;
	private String ticketId = "DEMO-1";
	private EnumStatus statuses;
	private String status = "Open";
	private StreamedContent file;
	private TreeNode root = new DefaultTreeNode("root", null);
	private Document selectedDocument;
	private Date dateFrom = null;
	private Date dateTo = null;

	private boolean showTicketTable = true;

	public StreamedContent downloadExcelReport() {

		ClassLoader classLoader = Thread.currentThread().getContextClassLoader();

		try {
			InputStream stream = ((ServletContext) FacesContext.getCurrentInstance().getExternalContext().getContext())
					.getResourceAsStream("JSONFolder/outPOI.xls");
			// InputStream stream =
			// classLoader.getResourceAsStream("JSONFolder/outPOI.xls");
			file = new DefaultStreamedContent(stream, "application/vnd.ms-excel", "report.xls");
			return file;
		} catch (ParsePropertyException e) {
			e.printStackTrace();
		}
		return file;
	}

	public void getTodayExpireIssues() {
		setShowTicketTable(true);
		root = new DefaultTreeNode("root", null);
		ticket = new ArrayList<IssueEntity>();
		List<String> ticketsKeys = jiraRestClientService.getAllCloseToExpireTickets();

		for (String key : ticketsKeys) {
			jiraService.fillDatabaseSingleTicket(key);
			System.out.println(key);

			IssueEntity test = service.getIssueByKey(key);
			ticket.add(test);
		}
		System.out.println("size = " + ticket.size());
		for (IssueEntity iss : ticket) {
			System.out.println(iss);
		}
		excelReportService.createReportPoi(ticket);
		excelReportService.createIssueTreeTable(root, ticket);
	}

	public void getAllTickets() {
		setShowTicketTable(true);
		root = new DefaultTreeNode("root", null);
		ticket = new ArrayList<IssueEntity>();
		jiraServiceFromTxt.fillDatabaseAllTickets();

	}

	public void getIssueByKey() {
		setShowTicketTable(true);
		root = new DefaultTreeNode("root", null);
		ticket = new ArrayList<IssueEntity>();
		IssueEntity issue = service.getIssueByKey(ticketId);

		if (issue != null) {
			ticket.add(issue);
			excelReportService.createReportPoi(ticket);
			excelReportService.createIssueTreeTable(root, ticket);
		} else {
		}

	}

	public void getIssueByStatus() {
		setShowTicketTable(true);
		root = new DefaultTreeNode("root", null);
		if (dateFrom != null || dateTo != null) {
			ticket = service.getIssuesByStatusAndDate(status, dateFrom, dateTo);
		} else {
			ticket = service.getIssuesByStatus(status);
		}
		if (ticket != null) {
			excelReportService.createReportPoi(ticket);
			excelReportService.createIssueTreeTable(root, ticket);
		} else {
		}

	}

	// login stuff
	public String getCurrentUsername() {
		User user = (User) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
		String name = user.getUsername(); // get logged in username
		return name;
	}

	public List<IssueEntity> getTicket() {
		return ticket;
	}

	public void setTicket(List<IssueEntity> ticket) {
		this.ticket = ticket;
	}

	public String getTicketId() {
		return ticketId;
	}

	public void setTicketId(String ticketId) {
		this.ticketId = ticketId;
	}

	public TicketService getService() {
		return service;
	}

	public void setService(TicketService service) {
		this.service = service;
	}

	public String getStatus() {
		return status;
	}

	public void setStatus(String status) {
		this.status = status;
	}

	public ExcelReportService getExcelReportService() {
		return excelReportService;
	}

	public void setExcelReportService(ExcelReportService excelReportService) {
		this.excelReportService = excelReportService;
	}

	public StreamedContent getFile() {
		return file;
	}

	public void setFile(StreamedContent file) {
		this.file = file;
	}

	public TreeNode getRoot() {
		return root;
	}

	public void setRoot(TreeNode root) {
		this.root = root;
	}

	public Document getSelectedDocument() {
		return selectedDocument;
	}

	public void setSelectedDocument(Document selectedDocument) {
		this.selectedDocument = selectedDocument;
	}

	public EnumStatus[] getStatuses() {
		return statuses.values();
	}

	public void setStatuses(EnumStatus statuses) {
		this.statuses = statuses;
	}

	public Date getDateFrom() {
		return dateFrom;
	}

	public void setDateFrom(Date dateFrom) {
		this.dateFrom = dateFrom;
	}

	public Date getDateTo() {
		return dateTo;
	}

	public void setDateTo(Date dateTo) {
		this.dateTo = dateTo;
	}

	public void handleDateFromSelect(SelectEvent event) {
		dateFrom = (Date) event.getObject();
		if (dateTo == null) {
			dateTo = new Date();
		}
	}

	public void handleDateToSelect(SelectEvent event) {
		dateTo = (Date) event.getObject();
	}

	public boolean isShowTicketTable() {
		return showTicketTable;
	}

	public void setShowTicketTable(boolean showTicketTable) {
		this.showTicketTable = showTicketTable;
	}

}
